package com.lsh.oms.provider.service.ro;

import com.alibaba.dubbo.config.annotation.Service;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.service.ro.IReturnOrderRestService;
import com.lsh.oms.core.model.order.OrderRoHead;
import com.lsh.oms.core.service.other.RoOrderService;
import com.lsh.oms.core.service.tool.Criteria;
import com.lsh.oms.provider.service.BaseService;

import javax.annotation.Resource;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

@Path("return-orders")
@Service(protocol = "rest", timeout = 30000)
@Deprecated
public class ReturnOrderRestServiceImpl extends BaseService implements IReturnOrderRestService {

    @Resource
    private RoOrderService returnOrderService;

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public CommonResult query(@Context UriInfo uriInfo,
                              @DefaultValue("1") @QueryParam("page") long page,
                              @DefaultValue("20") @QueryParam("page-limit") long limit) {
        return CommonResult.success(returnOrderService.query(Criteria.of(uriInfo), page, limit));
    }

    @GET
    @Path("{id}")
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public CommonResult findOne(@PathParam("id") Long id,
                                @HeaderParam("venderId") Long venderId,
                                @QueryParam("details-required") @DefaultValue(value = "false") boolean detailsRequired) {
        OrderRoHead returnOrder = returnOrderService.getRoHeadByReturnOrderId(id, detailsRequired);
        if (returnOrder == null || venderNotMatch(venderId, returnOrder)) {
            throw new NotFoundException();
        }
        return CommonResult.success(returnOrder);
    }

    private boolean venderNotMatch(Long venderId, OrderRoHead order) {
        return (venderId == null) || !venderId.equals(order.getVenderId());
    }
}
